//
//  STDSMaskKeyEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/6.
//

#ifndef STDSMaskKeyEffect_h
#define STDSMaskKeyEffect_h
#include "STDSBaseEffect.hpp"

class STDSMaskKeyEffect: public STDSBaseEffect {
public:
    STDSMaskKeyEffect() {
        mEffectType = STDSEffectTypeMaskKey;
    }
    ~STDSMaskKeyEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
//        STDSBaseEffect::initGL();
    }
    
    void setMaskTexture(shared_ptr<STDSTexture> tex) {
        mMaskTexture = tex;
    }
    
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mKeyProgram == nullptr) {
            mKeyProgram = mContext->getProgram(kShaderSRCMaskKey);
        }
        return mKeyProgram;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        
        if (program == nullptr || mMaskTexture == nullptr || mTexture == nullptr) {
            return;
        }
        glClearColor(0, 0, 0, 0);
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("uTexture"), 0);
        
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, mMaskTexture->getId());
        program->setUniform(program->getUniform("uMask"), 1);
        
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    shared_ptr<STDSTexture> mMaskTexture{nullptr};
    shared_ptr<STDSShaderProgram> mKeyProgram{nullptr};
};

#endif /* STDSMaskKeyEffect_h */
